<?php
namespace app\avikcm\controller;

use think\facade\Request;
use app\avikcm\model\Users as UsersModel;
class UsersAgent extends Common{
    //加盟列表
    public function index(){
        if(request()->isPost()){
            $key=input('post.key');
            $page =input('page')?input('page'):1;
            $pageSize =input('limit')?input('limit'):config('pageSize');
            $where[] = ['u.del','=',0];
            $where[] = ['u.user_type','=',3];
            if($key){
                $where[] = ['u.realname|u.mobile|u.nickname','like',"%".$key."%"];
            }
            $list=db('users')->alias('u')
                ->field('u.*')
                ->where($where)
                ->order('u.id desc')
                ->paginate(array('list_rows'=>$pageSize,'page'=>$page))
                ->toArray();
            if($list){
                $user_type_arr = config('user_type');
                foreach($list['data'] as $key=>$val){
                    $list['data'][$key]['user_type_name'] = $user_type_arr[$val['user_type']];
                }
            }
            return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
        }
        return $this->fetch();
    }

    //设置会员状态
    public function usersState(){
        $id=input('post.id');
        $is_lock=input('post.lock');
        if(db('users')->where('id='.$id)->update(['lock'=>$is_lock])!==false){
            return ['status'=>1,'msg'=>'设置成功!'];
        }else{
            return ['status'=>0,'msg'=>'设置失败!'];
        }
    }

    public function edit($id=''){
        if(request()->isPost()){
            $user = db('users');
            $brand = input('post.brand');
            $data = Request::except('brand');
            $create_time = date('Y-m-d H:i:s'); 
            if($brand){
                db('goods_brand_agent')->where('user_id', $data['id'])->delete();
                foreach($brand as $key=>$val){
                    db('goods_brand_agent')->insert(['user_id'=> $data['id'], 'brand_id'=> $key, 'create_time'=>$create_time,'admin_id'=>session('aid')]);
                }
            }

            if ($user->update($data)!==false) {
                $result['msg'] = '会员修改成功!';
                $result['url'] = url('index');
                $result['code'] = 1;
            } else {
                $result['msg'] = '会员修改失败!';
                $result['code'] = 0;
            }
            return $result;
        }else{
            $info = UsersModel::get($id);
            $brand = db('goods_category')->where(['del' => 0, 'pid' => 0])->select();
            $brand_agent = db('goods_brand_agent')->where('user_id', $id)->select();
            $agent = [];
            if($brand_agent){
                foreach($brand_agent as $val){
                    $agent[] = $val['brand_id'];
                }
            }
            $this->assign('user_type', config('user_type'));
            $this->assign('brand', $brand);
            $this->assign('agent', $agent);
            $this->assign('info',json_encode($info,true));
            $this->assign('title',lang('edit').lang('user'));
            return $this->fetch();
        }
    }

    public function usersDel(){
        db('users')->where(['id'=>input('id')])->update(['del'=>1]);
        return $result = ['code'=>1,'msg'=>'删除成功!'];
    }

    public function getRegion(){
        $Region=db("region");
        $pid = input("pid");
        $arr = explode(':',$pid);
        $map['pid']=$arr[1];
        $list=$Region->where($map)->select();
        return $list;
    }

}